<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>人员列表--layui后台管理模板 2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="../../layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="../../css/public.css" media="all" />
</head>

<body class="childrenBody">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <div class="layui-input-inline">
                <input id="name" placeholder="输入姓名" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <select id="dept">
                    <option value="">选择机构</option>
                    <option value="物流">物流</option>
                    <option value="科室">科室</option>
                </select>
            </div>

            <div class="layui-input-inline">
                <select id="duty">
                    <option value="">选择职位</option>
                    <option value="配送员">配送员</option>
                    <option value="护士">护士</option>
                </select>
            </div>

            <div class="layui-inline">
                <a class="layui-btn search_btn"><i class="layui-icon">&#xe615;</i>查询</a>
            </div>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-normal addEmployee_btn"><i class="layui-icon">&#xe654;</i>添加人员</a>
            </div>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-normal batchImport_btn" id="batchImport_btn"><i
                        class="layui-icon">&#xe62f;</i>批量导入</a>
            </div>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-danger delAll_btn"><i class="layui-icon">&#xe640;</i>批量删除</a>
            </div>
        </form>
    </blockquote>

    <table id="employeeList" lay-filter="employeeList"></table>

    <script type="text/html" id="employeeListBar">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon">&#xe642;</i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i class="layui-icon">&#xe640;</i>删除</a>
        </script>

    <script type="text/javascript" src="../../layui/layui.js"></script>
    <script type="text/javascript" src="../../js/common.js"></script>
    <script>
        layui.use(['form', 'layer', 'laydate', 'table', 'laytpl', 'upload'], function () {
            var form = layui.form,
                layer = parent.layer === undefined ? layui.layer : top.layer,
                $ = layui.jquery,
                laydate = layui.laydate,
                laytpl = layui.laytpl,
                table = layui.table,
                upload = layui.upload;

            //指定允许上传的文件类型
            upload.render({
                elem: '#batchImport_btn'
                , url: window.document.location.href + 'sys/employee/upload'
                , accept: 'file' //普通文件
                , exts: 'xlsx' //允许上传的文件后缀
                // , multiple: true
                , done: function (data) {
                    if (data.code == 0) {
                        layer.msg("数据导入成功！");
                        location.reload();
                    } else {
                        layer.alert(data.msg);
                    }
                }
            });

            //第一个实例
            var tableIns = table.render({
                elem: '#employeeList'
                , url: contextPath + '/sys/employee/queryList' //数据接口
                , id: 'employeeListTable'
                , page: true //开启分页
                , limit: 15
                , limits: [15, 30]
                , even: true
                , height: "full-125"
                , method: 'POST'   //laui 修改请求方式
                , cellMinWidth: 95
                , cols: [[ //表头
                    { type: 'checkbox', fixed: 'left' }
                    , { field: 'id', title: 'ID', align: "center" } //width 支持：数字、百分比和不填写。你还可以通过 minWidth 参数局部定义当前单元格的最小宽度，layui 2.2.1 新增
                    , {
                        title: '头像', align: "center", templet: function (d) {
                            return '<img src="../../images/face.png" class="layui-nav-img userAvatar">';
                        }
                    }
                    , { field: 'name', title: '名称', align: "center" }
                    , {
                        field: 'sex', title: '性别', align: "center", templet: function (d) {
                            if (d.sex == "男") {
                                return "<i class='layui-icon' style='color:green;margin-right:5px'>&#xe662;</i>";
                            } else if (d.sex == "女") {
                                return "<i class='layui-icon' style='color:red;margin-right:5px'>&#xe661;</i>";
                            } else if (d.sex == "保密") {
                                return d.sex;
                            }
                        }
                    }
                    , { field: 'rfid', title: 'RFID', width: 250, align: "center" }
                    , { field: 'dept', title: '机构', align: "center" }
                    , { field: 'duty', title: '职务', align: "center" }
                    , { field: 'phone', title: '联系方式', width: 150, sort: true }
                    , { title: '操作', templet: '#employeeListBar', width: 160, fixed: "right", align: "center" }
                ]]
            });

            //搜索
            $(".search_btn").on("click", function () {
                table.reload('employeeListTable', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        name: $('#name').val(),
                        dept: $('#dept').val(),
                        duty: $('#duty').val(),
                    }
                });

                return false;
            });

            //新增
            $(".addEmployee_btn").on("click", function () {
                addEmployee();
            });

            //批量删除
            $(".delAll_btn").click(function () {
                var checkStatus = table.checkStatus('employeeListTable'),
                    data = checkStatus.data,
                    employeeIds = [];
                if (data.length > 0) {
                    for (var i in data) {
                        employeeIds.push(data[i].id);
                    }
                    layer.confirm('确定删除选中的人员？', { icon: 3, title: '提示信息' }, function (index) {
                        layer.close(index);
                        delEmployee(employeeIds);
                    })
                } else {
                    layer.msg("请选择需要删除的人员");
                }
            })

            //新增/编辑人员
            function addEmployee(edit) {
                var title;
                if (edit) {
                    title = "编辑人员";
                } else {
                    title = "添加人员";
                }

                var index = layui.layer.open({
                    title: title,
                    type: 2,
                    content: "employeeAdd.html",
                    success: function (layero, index) {
                        var body = layui.layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.initData(edit);

                        setTimeout(function () {
                            layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                                tips: 3
                            });
                        }, 500)
                    }
                })
                layui.layer.full(index);
                window.sessionStorage.setItem("index", index);
                //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
                $(window).on("resize", function () {
                    layui.layer.full(window.sessionStorage.getItem("index"));
                })
            }

            //删除人员
            function delEmployee(employeeIds) {
                $.ajax({
                    url: contextPath + '/sys/employee/delete',
                    type: 'post',
                    dataType: 'json',
                    data: "id=" + employeeIds,
                    beforeSend: function () {
                        i = showLoad();
                    },
                    success: function (data) {
                        closeLoad(i);

                        if (data.code == 0) {
                            layer.msg("人员删除成功！");

                            //刷新表格
                            $('.search_btn').click();
                        } else {
                            layer.alert(data.msg);
                        }
                    },
                    error: function (data) {
                        closeLoad(i);
                    }
                });
            }

            //监听工具条
            table.on('tool(employeeList)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                // debugger;
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的DOM对象

                switch (layEvent) {
                    case 'edit':
                        addEmployee(data);
                        break;
                    case 'delete':

                        layer.confirm('确定删除此条记录？', { icon: 3, title: '提示信息' }, function (index) {
                            layer.close(index);

                            var employeeIds = [];
                            employeeIds.push(data.id);
                            delEmployee(employeeIds);
                        });
                        break;
                }
            });
        });

    </script>
</body>

</html>